package com.bw.service.customer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.entity.customer.PiCustomer;
import com.bw.entity.dc.PiLinkman;
import com.bw.mapper.customer.PiCustomerMapper;
import com.bw.resp.ResponseResult;
import com.bw.service.customer.IPiCustomerService;
import com.bw.service.dc.IPiLinkmanService;
import com.bw.utils.SendSms;
import com.bw.mapper.dc.PiLinkmanMapper;
import com.bw.resp.ResponseResult;
import com.bw.service.customer.IPiCustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wsq
 * @since 2020-06-16
 */
@Service
public class PiCustomerServiceImpl extends ServiceImpl<PiCustomerMapper, PiCustomer> implements IPiCustomerService {

    @Autowired
    private StringRedisTemplate redis;
    @Autowired
    private PiLinkmanMapper linkmanMapper;
    private final Logger log= LoggerFactory.getLogger(PiCustomerServiceImpl.class);

    /**
     * 发送验证码
     * @param tel
     * @return
     */
    @Override
    public ResponseResult sendYzm(String tel) {
        log.info("发送验证码开始");
        ResponseResult re = new ResponseResult(false, 400, "发送失败");
        try {
            //-------------------用户是否存在
            QueryWrapper qw = new QueryWrapper();
            if(tel!=null) {
                qw.eq("TEL", tel);
            }
            List<PiLinkman> linkmenlist=linkmanMapper.selectList(qw);
            if(linkmenlist==null || linkmenlist.size()==0) {
                re.setCode(400);
                re.setSuccess(false);
                re.setMessage("客户不存在");
                log.info("客户不存在");
                return re;
            }
            //----------------------发送频繁(s+tel作为key,存入验证码(过期时间一分钟))
            Boolean bo = redis.boundValueOps("s"+tel).setIfAbsent("yzm", 1, TimeUnit.MINUTES);
            if(!bo) {
                re.setSuccess(false);
                re.setCode(400);
                re.setMessage("发送验证码过于频繁");
                log.info("发送验证码过于频繁");
                return re;
            }

            //-------------------发送上限(p+tel作为key，判断次数)
            String num=redis.boundValueOps("p"+tel).get();//获取发送的次数
            if(num!=null) {
                if(Integer.valueOf(num)>10) {//最多发送10次
                    re.setSuccess(false);
                    re.setCode(400);
                    re.setMessage("发送验证码次数已达上限");
                    log.info("发送验证码次数已达上限");
                    return re;
                }
            }else {
                //发送第一次，设置生命周期为1天
                redis.boundValueOps("p"+tel).expire(1, TimeUnit.DAYS);
            }
            //发送成功次数+1
            redis.boundValueOps("p"+tel).increment();
            //-----------------发送验证码
            int code=(int)(Math.floor(Math.random()*1000000));//yzm+tel为key+验证码
            redis.boundValueOps("yzm"+tel).set(String.valueOf(code), 5, TimeUnit.MINUTES);//过期时间5分钟
            re.setSuccess(true);
            re.setCode(200);
            re.setMessage("发送成功");
            log.info("发送验证码结束,验证码="+code);
            //调用阿里大于发送短信
          //  SendSms.send(tel, String.valueOf(code));
            re.setMessage("短信已发送，请注意查收");
            log.info("短信发送成功");
            return re;
        } catch (Exception e) {
            log.error("发送验证码失败,{}",e.toString());
            return re;
        }
    }

}
